<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::post('/tokens/create', function (\App\Api\Contracts\ApiResponseContract $api, \App\Api\Contracts\VerifyReqContract $ver) {
    $params = $ver->checkParams(['name', 'password']);
    $user = \App\Models\User::where(['name' => $params['name']])->first();
    if (!$user || !Hash::check($params['password'], $user['password'])) {
        return $api->fail(message: "帐号或密码错误，请重新输入");
    }

    return $api->success(datas:['user'=>$user,'token' => $user->createToken('my-app-token')->plainTextToken]);
});

Route::post('create/user', function (\App\Api\Contracts\ApiResponseContract $api, \App\Api\Contracts\VerifyReqContract $ver) {
    $params = $ver->checkParams(['name', 'password', 'email']);
    $params['password'] = \Illuminate\Support\Facades\Crypt::encrypt($params['password']);
    return $api->success(datas: \App\Models\User::create($params));
});

Route::middleware('auth:sanctum')->get('test/token',function (App\Api\Contracts\ApiResponseContract $api){
    return $api->success();
});
